package com.gusha.controller;

import java.math.BigInteger;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gusha.config.SysOperateLog;
import com.gusha.dao.InfoContentDao;
import com.gusha.dao.SysCommentDao;
import com.gusha.dao.SysReportDao;
import com.gusha.dao.SysTrainingDao;
import com.gusha.dao.TrainCommDao;
import com.gusha.model.InfoContent;
import com.gusha.model.Project;
import com.gusha.model.SysComment;
import com.gusha.model.SysProducts;
import com.gusha.model.SysReport;
import com.gusha.model.SysTraining;
import com.gusha.model.TrainComm;
import com.gusha.service.SysReportService;
import com.gusha.utils.Constants;
import com.gusha.utils.JsonResult;
import com.gusha.utils.UpdateUtil;
import com.gusha.utils.WebValueUtil;

@Controller
@RequestMapping("/gx/sysReport")
public class SysReportController {
	@Autowired
	private SysReportDao sysReportDao;
	@Autowired
	private SysReportService sysReportService;
	@Autowired
	private InfoContentDao infoContentDao;
	@Autowired
	private SysTrainingDao sysTrainingDao;
	@Autowired
	private SysCommentDao sysCommentDao;
	@Autowired
	private TrainCommDao trainCommDao;
	
    @RequestMapping("list")
    @SysOperateLog(operate = "查看举报列表")
	public String list(Model model, HttpServletRequest request) {
    	 String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
    	 String contentType= WebValueUtil.getWebValue(request, "contentType");
         int pageNo = 0;
         if (StringUtils.isNotBlank(pageIndex)) {
             pageNo = Integer.parseInt(pageIndex);
         }
        SysReport report = new SysReport();
        if(StringUtils.isNotBlank(contentType)) {
        	report.setContentType(new BigInteger(contentType));
        }
         
         Sort sort = new Sort(Sort.Direction.DESC, "reportTime");
         Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
         Page<SysReport> page = sysReportService.findAll(pageable, report);
         
         model.addAttribute("page", page);
         
		return "/admin/sysReport/list";
		
	}
    @RequestMapping("toEdit")
    @SysOperateLog(operate = "进入举报审核")
    public String toEdit(Model model,HttpServletRequest request) {
    	String id= WebValueUtil.getWebValue(request, "id");
    	if(StringUtils.isNotBlank(id)) {
    		SysReport report= sysReportDao.getOne(new BigInteger(id));
    		if(report.getContentType().toString().equals("1")) {
    			InfoContent info=infoContentDao.getOne(report.getContentId());
    			model.addAttribute("obj",info);
    			model.addAttribute("rep",report);
    			return "/admin/sysReport/check1";
    		}//资讯
    		if(report.getContentType().toString().equals("2")) {
    			SysTraining train=sysTrainingDao.getOne(report.getContentId());
    			model.addAttribute("obj",train);
    			model.addAttribute("rep",report);
    			return "/admin/sysReport/check2";
    		}//培训
    		if(report.getContentType().toString().equals("3")) {
    			SysComment info=sysCommentDao.getOne(report.getContentId());
    			model.addAttribute("obj",info);
    			model.addAttribute("rep",report);
    			return "/admin/sysReport/check3";
    		}
    		if(report.getContentType().toString().equals("4")) {
    			TrainComm tComm=trainCommDao.getOne(report.getContentId());
    			model.addAttribute("obj",tComm);
    			model.addAttribute("rep",report);
    			return "/admin/sysReport/check4";
    		}
    		
    		
    		
    	}
		return null;
		
    	
    }
    
    @RequestMapping("doCheck")
    @ResponseBody
    @SysOperateLog(operate = "审核项目")
    public JsonResult doCheck(SysReport report) {
        JsonResult result = new JsonResult(false,"审核失败");
        if (report.getId() != null) {
        	SysReport sysReport = sysReportDao.getOne(report.getId());
            UpdateUtil.copyNonNullProperties(report, sysReport);
        }
        sysReportDao.saveAndFlush(report);
        result.setFlag(true);
        result.setMsg("审核成功");

        return result;
    }
}
